/*
 * Copyright 2008 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.zxing;

import com.google.zxing.common.ByteMatrix;
import com.google.zxing.oned.EAN13Writer;
import com.google.zxing.oned.EAN8Writer;
import com.google.zxing.oned.Code128Writer;
import com.google.zxing.oned.Code39Writer;
import com.google.zxing.oned.ITFWriter;
import com.google.zxing.qrcode.QRCodeWriter;

import java.util.Hashtable;
import android.util.Log;

/**
 * This is a factory class which finds the appropriate Writer subclass for the BarcodeFormat
 * requested and encodes the barcode with the supplied contents.
 *
 */
public final class MultiFormatWriter implements Writer {

  public ByteMatrix encode(String contents, BarcodeFormat format, int width,
      int height) throws WriterException {

    return encode(contents, format, width, height, null);
  }

  public ByteMatrix encode(String contents, BarcodeFormat format, int width, int height,
      Hashtable hints) throws WriterException {

    if (format == BarcodeFormat.EAN_8) {
      return new EAN8Writer().encode(contents, format, width, height, hints);
    } else if (format == BarcodeFormat.EAN_13) {
      return new EAN13Writer().encode(contents, format, width, height, hints);
    } else if (format == BarcodeFormat.UPC_A) {
      return new EAN13Writer().encode("0"+contents, BarcodeFormat.EAN_13, width, height, hints);
    } else if (format == BarcodeFormat.UPC_E){
      //instead of generating upc e formats, convert it to a upc a number and generate that
      if(contents.length()!=8)
    	  throw new IllegalArgumentException("No encoder available for format " + format);
      else
      {
    	  char back=contents.charAt(7);
    	  contents=contents.substring(1,7);
    	  char temp=contents.charAt(5);
    	  String newContent="";
    	  
    	  if(temp=='0' || temp=='1' || temp=='2')
    	  {
    		  newContent=newContent+contents.substring(0,2)+temp+"0000"+contents.substring(2,5);
    	  }
    	  else if(temp=='3')
    	  {
    		  newContent=newContent+contents.substring(0,3)+"00000"+contents.substring(3,5);
    	  }
    	  else if(temp=='4')
    	  {
    		  newContent=newContent+contents.substring(0,4)+"00000"+contents.substring(4,5);
    	  }
    	  else if(temp=='5' || temp=='6' || temp=='7' || temp=='8' || temp=='9')
    	  {
    		  newContent=newContent+contents.substring(0,5)+"0000"+temp;
    	  }
    	  newContent="00"+newContent+back;
    	  return new EAN13Writer().encode(newContent, BarcodeFormat.EAN_13, width, height, hints);
      }
    } else if (format == BarcodeFormat.QR_CODE) {
      return new QRCodeWriter().encode(contents, format, width, height, hints);
    } else if (format == BarcodeFormat.CODE_128) {
    	return new Code128Writer().encode(contents, BarcodeFormat.CODE_128, width, height, hints);
    } else if (format == BarcodeFormat.CODE_39) {
    	return new Code39Writer().encode(contents, BarcodeFormat.CODE_39, width, height, hints);
    } else if (format == BarcodeFormat.ITF) {
    	if(contents.length()%2==1)
    		contents="0"+contents;
    	return new ITFWriter().encode(contents, BarcodeFormat.ITF, width, height, hints);
    } else {
      throw new IllegalArgumentException("No encoder available for format " + format);
    }
  }

}
